﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Artem.Google.UI;
using GeoFramework;
using Subgurim.Controles;
using VO.Business.Controllers;
using VO.Business.Utils;
using VO.Entities;
using VO.Web.Utils;

namespace VO.Web
{
    public partial class TrackDetails : System.Web.UI.Page
    {
        private Track _currentTrack;
        protected Track CurrentTrack
        {
            get
            {
                if (_currentTrack == null)
                {
                    if (Page.RouteData.Values["trackId"] != null)//routing tracks
                    {
                        string trackId = Page.RouteData.Values["trackId"] as string;
                        _currentTrack = TracksController.GetTrack(new Guid(trackId));
                        TrackCreator = UsersController.GetUser(_currentTrack.UserId);
                    }
                }
                return _currentTrack;
            }
            set { _currentTrack = value; }
        }

        private TrackDone _doneTrack;
        protected TrackDone DoneTrack
        {
            get
            {
                if (_doneTrack == null)
                {
                    if (Page.RouteData.Values["doneTrackId"] != null)//routing mydonetracks
                    {
                        string doneTrackId = Page.RouteData.Values["doneTrackId"] as string;
                        _doneTrack = TracksDonesController.GetTrackDone(new Guid(doneTrackId));
                        CurrentTrack = TracksController.GetTrack(_doneTrack.TrackId);
                        TrackCreator = UsersController.GetUser(CurrentTrack.UserId);
                    }
                }
                return _doneTrack;
            }
            set { _doneTrack = value; }
        }

        private User _trackCreator;
        protected User TrackCreator
        {
            get { return _trackCreator; }
            set { _trackCreator = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GMap1.setCenter(new GLatLng(0.01, 0.01), 1);

                GMapUIOptions options = new GMapUIOptions();
                options.maptypes_hybrid = false;
                options.keyboard = true;
                options.maptypes_physical = true;
                options.zoom_scrollwheel = true;
                options.controls_smallzoomcontrol3d = true;
                options.controls_largemapcontrol3d = false;
                options.controls_scalecontrol = false;
                GMap1.Add(new GMapUI(options));

                if (Page.RouteData.Values["trackId"] != null)//routing tracks
                {
                    //string trackId = Page.RouteData.Values["trackId"] as string;
                    //Track track = TracksController.GetTrack(new Guid(trackId));
                    Track track = CurrentTrack;//dummy
                    DrawTrack(track);
                }
                else if (Page.RouteData.Values["doneTrackId"] != null)//routing mydonetracks
                {
                    //string doneTrackId = Page.RouteData.Values["doneTrackId"] as string;
                    //TrackDone doneTrack = TracksDonesController.GetTrackDone(new Guid(doneTrackId));
                    //Track track = TracksController.GetTrack(doneTrack.TrackId);
                    TrackDone doneTrack = DoneTrack;//to fill
                    DrawTrack(CurrentTrack);
                    //TODO - display some other stuff of done track
                    pnlDoneTrackDetail.Visible = true;
                    lblPostedDate.Text = DoneTrack.PostedDate.ToShortDateString();
                    lblStartTime.Text = DoneTrack.StartTime.ToString();
                    lblEndtime.Text = DoneTrack.EndTime.ToString();
                    lblCompleteTime.Text = DateTimeUtils.GetCompletedTimeString(DoneTrack.CompletedTime);
                }
            }
        }

        private void DrawTrack(Track track)
        {
            if (track != null)
            {
                //
                lblTrackName.Text = track.TrackName;
                //TrackCreator = UsersController.GetUser(track.UserId);
                lblOwnerUser.Text = TrackCreator.PublicName;
                lblTrackCategory.Text = TracksCategoriesController.GetTrackCategory(track.TrackCategoryId).CategoryName;

                if (!string.IsNullOrEmpty(track.GeoString))
                {
                    //draw track
                    GPolyline line = new GPolyline();
                    line.colorNet = Color.Fuchsia;
                    line.opacity = 1;
                    line.weight = 3;
                    List<LocationPoint> locationPoints = GeoUtils.DeserializeTrack(track.GeoString);
                    lblCheckPoints.Text = locationPoints.Count.ToString();
                    lblTrackLength.Text = string.Format("{0:0.00} Km", track.TrackLength);
                        //TODO - Metric and Imperial system

                    GooglePolyline polLine = new GooglePolyline();
                    GMap1.markerManager = new MarkerManager();
                    int counter = 0;
                    int pointcount = locationPoints.Count;
                    locationPoints.ForEach(delegate(LocationPoint point)
                                               {
                                                   GMarker marker =
                                                       new GMarker(new GLatLng(point.Latitude, point.Longitude));
                                                   marker.ID = "P_" + counter.ToString();
                                                   GMarkerOptions opt = new GMarkerOptions();
                                                   opt.title = counter == 0
                                                                   ? "Start Point"
                                                                   : counter == pointcount - 1
                                                                         ? "Finish Point"
                                                                         : "Check point no " + counter;
                                                   GIcon icon = new GIcon();
                                                   icon.iconSize = new GSize(32, 32);
                                                   icon.iconAnchor = new GPoint(16, 16);
                                                   icon.shadowSize = new GSize(0, 0);
                                                   opt.icon = icon;
                                                   opt.icon.image = counter == 0
                                                                        ? "/images/start_point.png"
                                                                        : counter == pointcount - 1
                                                                              ? "/images/finish_point.png"
                                                                              : "/images/control_point.png";
                                                   marker.options = opt;
                                                   GMap1.markerManager.Add(marker, 5);

                                                   line.Add(new GLatLng(point.Latitude, point.Longitude));
                                                   polLine.Points.Add(new GoogleLocation(point.Latitude, point.Longitude));
                                                   counter++;
                                               });
                    GMap1.addPolyline(line);

                    double latMin;
                    double latMax;
                    double lonMin;
                    double lonMax;
                    double latCenter;
                    double lonCenter;
                    GeoUtils2.GetCenterAndBounds(polLine, out latMin, out latMax, out lonMin, out lonMax, out latCenter,
                                                 out lonCenter);

                    GLatLng center = new GLatLng(latCenter, lonCenter);
                    GLatLngBounds bounds = new GLatLngBounds(new GLatLng(latMin, lonMin), new GLatLng(latMax, lonMax));
                    int zoom = GMap1.getBoundsZoomLevel(bounds);
                    GMap1.setCenter(center, zoom);
                }
            }
        }
    }
}